package com.three.service.impl;

import com.baor.baseUtil.R;
import com.three.dao.SubjectsDao;
import com.three.domain.Subjects;
import com.three.service.SubjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class SubjectsServiceImpl implements SubjectsService {
    @Autowired
    private SubjectsDao subjectsDao;

    @Override
    public R subjectsList(Integer pageIndex, Integer pageSize,String keyword) {
        System.out.println(pageIndex);
        System.out.println(pageSize);
        pageIndex = (pageIndex - 1) * pageSize;
        double pageCountSubjects = subjectsDao.pageCountSubjects(keyword);
        System.out.println(pageCountSubjects);
        pageCountSubjects = Math.ceil(pageCountSubjects / pageSize);
        List<Subjects> subjectsList = subjectsDao.subjectsList(pageIndex, pageSize,keyword);
        Map<String, Object> map = new HashMap<>();
        map.put("pageCountSubjects", pageCountSubjects);
        map.put("attendanceList", subjectsList);
        return R.ok(map);
    }

    @Override
    public R addSubjects(Subjects subjects) {
        return R.ok(subjectsDao.addSubjects(subjects));
    }

    @Override
    public R updateSubjects(Subjects subjects) {
        return R.ok(subjectsDao.updateSubjects(subjects));
    }

    @Override
    public R deleteSubjects(Integer subject_id) {
        return R.ok(subjectsDao.deleteSubjects(subject_id));
    }
}
